<?php

namespace app\common\model;

use think\Model;

/**
 * 会员模型
 */
class User extends Model
{

    // 开启自动写入时间戳字段
    protected $autoWriteTimestamp = 'int';
    // 定义时间戳字段名
    protected $createTime = 'createtime';
    protected $updateTime = 'updatetime';
    // 追加属性
    protected $append = [

    ];

    //1=爆品,2=定点捐增,3=捐闲置,4=注册,5=拉人,6=兑换,7=其他
    const STATUS=[1=>'爆品',2=>'定点捐增',3=>'闲置',4=>'注册',5=>'结缘',6=>'兑换',7=>'退款'];
    /**
     * 获取头像
     * @param   string $value
     * @param   array  $data
     * @return string
     */
    public function getAvatarAttr($value, $data)
    {
        if ($value){
            $value=get_app_image_url($data['avatar']);
        }
        return $value;
    }
    /**
     * 获取会员的组别
     */
    public function getGroupAttr($value, $data)
    {
        return UserGroup::get($data['group_id']);
    }

    /**
     * 获取验证字段数组值
     * @param   string $value
     * @param   array  $data
     * @return  object
     */
    public function getVerificationAttr($value, $data)
    {
        $value = array_filter((array)json_decode($value, true));
        $value = array_merge(['email' => 0, 'mobile' => 0], $value);
        return (object)$value;
    }

    /**
     * 设置验证字段
     * @param mixed $value
     * @return string
     */
    public function setVerificationAttr($value)
    {
        $value = is_object($value) || is_array($value) ? json_encode($value) : $value;
        return $value;
    }

    /**
     * 变更会员余额
     * @param int    $money   余额
     * @param int    $user_id 会员ID
     * @param string $memo    备注
     * @param string $type    1收入2支出
     */
    public static function money($money, $user_id, $memo,$type)
    {
        $user = self::get($user_id);
        if ($user && $money != 0) {
            $before = $user->money;
            //$after = $user->money + $money;
            if ($type==1){
                $after = function_exists('bcadd') ? bcadd($user->money, $money, 2) : $user->money + $money;
            }else{
                $after = function_exists('bcsub') ? bcsub($user->money, $money, 2) : $user->money - $money;
                if ($after<0){
                    return false;
                }
            }
            //更新会员信息
            $user->save(['money' => $after]);
            //写入日志
            MoneyLog::create(['user_id' => $user_id, 'money' => $money, 'before' => $before, 'after' => $after, 'memo' => $memo]);
            return true;
        }
        return false;
    }


    /**
     * 变更会员福报种子
     * @param int    $send   种子
     * @param int    $user_id 会员ID
     * @param string $memo    备注
     * @param string $type    1收入2支出
     * @param string $status  类型:1=爆品,2=定点捐增,3=捐闲置,4=注册,5=拉人,6=兑换,7=其他
     */
    public static function send($send, $user_id,$status=1,$type=1)
    {
        $user = self::get($user_id);
        if($type==2){
            if($user['send']<$send){
                return false;
            }
        }
        if ($user && $send != 0) {
            $before = $user->send;
            //$after = $user->money + $money;
            if ($type==1){
                $after = function_exists('bcadd') ? bcadd($user->send, $send, 2) : $user->send + $send;
                $data['total_send']=$after;
            }else{
                $after = function_exists('bcsub') ? bcsub($user->send, $send, 2) : $user->send - $send;
                if ($after<0){
                    return false;
                }
            }
            $data['send']=$after;

            //更新会员信息
            $user->save($data);
            //写入日志
            $memo = self::STATUS[$status];
            SendLog::create(['user_id' => $user_id, 'send' => $send, 'before' => $before, 'after' => $after, 'memo' => $memo,'type'=>$type,'status'=>$status]);
            return true;
        }
        return false;
    }


    /**
     * 变更会员积分
     * @param int    $score   积分
     * @param int    $user_id 会员ID
     * @param string $memo    备注
     */
    public static function score($score, $user_id, $memo)
    {
        $user = self::get($user_id);
        if ($user && $score != 0) {
            $before = $user->score;
            $after = $user->score + $score;
            $level = self::nextlevel($after);
            //更新会员信息
            $user->save(['score' => $after, 'level' => $level]);
            //写入日志
            ScoreLog::create(['user_id' => $user_id, 'score' => $score, 'before' => $before, 'after' => $after, 'memo' => $memo]);
        }
    }

    /**
     * 根据积分获取等级
     * @param int $score 积分
     * @return int
     */
    public static function nextlevel($score = 0)
    {
        $lv = array(1 => 0, 2 => 30, 3 => 100, 4 => 500, 5 => 1000, 6 => 2000, 7 => 3000, 8 => 5000, 9 => 8000, 10 => 10000);
        $level = 1;
        foreach ($lv as $key => $value) {
            if ($score >= $value) {
                $level = $key;
            }
        }
        return $level;
    }

//    /**
//     * 根据累计种子获取等级
//     * @param int $total_send 累计种子
//     * @return int
//     */
//    public static function getTotalSendAttr($total_send = 0)
//    {
////        $lv = array(1 => 0, 2 => 30, 3 => 100, 4 => 500, 5 => 1000, 6 => 2000, 7 => 3000, 8 => 5000, 9 => 8000, 10 => 10000);
//        $set = get_set();
//        $lv = explode(',',$set['total_send']);
//        $level = 1;
//        foreach ($lv as $key => $value) {
//            if ($total_send >= $value) {
//                $level = $key;
//            }
//        }
//        return $level;
//    }

    /**
     * 根据累计种子获取等级
     * @param int $total_send 累计种子
     * @return int
     */
    public static function getTotalSendAttr($value, $data)
    {
        $total_send = $value ? $value : (isset($data['total_send']) ? $data['total_send'] : '');
        $set = get_set();
        $lv  = explode(',',$set['total_send']);
        $level = 0;
        foreach ($lv as $key => $value) {
            if ($total_send >= $value) {
                $level = $key;
            }
        }
        $k = ['A','B','C','D','E','F','G','H','I','J','K','L','M','N','O','P','Q','R','S','T','U','V','W','X','Y','Z'];
        return '捐赠等级'.$k[$level];
    }
    /**
     * 根据累计种子获取等级
     * @param int $total_send 累计种子
     * @return int
     */
    public static function getTotalInviteAttr($value, $data)
    {
        $total_send = $value ? $value : (isset($data['total_invite']) ? $data['total_invite'] : '');
        $set = get_set();
        $lv  = explode(',',$set['total_invite']);
        $level = 0;
        foreach ($lv as $key => $value) {
            if ($total_send >= $value) {
                $level = $key;
            }
        }
        $k = ['A','B','C','D','E','F','G','H','I','J','K','L','M','N','O','P','Q','R','S','T','U','V','W','X','Y','Z'];
        return '职位等级'.$k[$level];
    }

    public function getImagesAttr($value, $data)
    {
        $value = $value ? $value : (isset($data['images']) ? $data['images'] : '');
        if(!$value){ return  [];}
        $value = explode(',',$value);
        foreach ($value as $k=>&$v){
            $v=get_app_image_url($v);
        }
        return $value;
    }

    /**
     * 我的团队
     * */
    public static function myfans($user_id,$field='*'){
        $where['pid']=$user_id;
        $list['list'] =   self::where($where)
                ->field($field)
                ->paginate(5);
        $list['count'] =   User::where($where)
                ->field($field)
                ->count();
        return $list;
    }
}
